<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<script src="{{__PUBLIC__}}/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<style type="text/css">
		li span{
			display: inline-block;
			cursor: pointer;
		}
	</style>
</head>
<body>
	<a href="javascript:history.go(-1);">向上一页</a>
	<ul>
	{{foreach from="$data" key="$k" value="$v"}}
		<li>
			<img src="{{$v['pic']}}" style="width:50px;"/>
			{{$v['name']}}
			{{$v['gprice']}}
			<span onclick="jian(this,{{$v['id']}})">[-]</span>
			<input type="text" name="num" value="0" id="{{$v['id']}}"/>
			<span onclick="jia(this,{{$v['id']}})">[+]</span>
		</li>
	{{endforeach}}
	</ul>
	数量：<span id="total"></span> 
	<a href="javascript:send();">结算</a> 
	<script type="text/javascript">
		function send(){
			var cart = getCart();
			$.post('{{U("saveCart")}}',cart,function(data){
				location.href="{{U('order')}}";
			})
		}
	</script>
	
	<script type="text/javascript">
	
		function jia(obj,id){
			var input=$(obj).parent().find('input');
			var num=input.val()*1+1;
			input.val(num)
			//购物车处理
			setCart(id,num);
			changeTotal();
		}
		
		function jian(obj,id){
			var input=$(obj).parent().find('input');
			var num=input.val()*1-1;
			if(num<=0)
			{
				num = 0;
			}
			input.val(num)
			//购物车处理
			setCart(id,num);
			changeTotal();
		}
	
		$(function(){
			changeTotal();
			var cart = getCart();
			//更改商品数量
			for(var i in cart.data){
				$("#"+i).val(cart.data[i]);
			}
		})
		
		//设置页面中的显示数量
		function changeTotal(){
			var cart = getCart();
			$("#total").html(cart.total);
			
		}
		
		//获取购物车清单
		function getCart(){
			var cart = localStorage.getItem('cart');
			if(!cart)
			{
				cart={data:{},total:0};
			}		
			else
			{
				cart = jQuery.parseJSON(cart);
			}	
			return cart;
		}
		//加商品添加到购物车
		function setCart(id,num){
			var cart = getCart();
			cart.data[id]=num;
			cart.total+=num;
			localStorage.setItem('cart',JSON.stringify(cart));
		}
	</script>
</body>
</html>











